#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Kang.Cunhua
# @Date:   2015-12-21 15:39:48
# @Last Modified by:   Kang.Cunhua
# @Last Modified time: 2015-12-21 16:50:08
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait  # available since 2.4.0
import time

# Create a new instance of the Firefox driver
driver = webdriver.Chrome()

# go to the baidu home page
driver.get("http://www.baidu.com")

# find the element that's name attribute is wd (the baidu search box)
inputElement = driver.find_element_by_name("wd")

# type in the search
inputElement.send_keys("Cheese!")

# submit the form. (although google automatically searches now without submitting)
inputElement.submit()

# the page is ajaxy so the title is originally this:
# print driver.title

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(lambda driver: driver.title.lower().startswith("cheese!"))

    # You should see "cheese!_百度搜索"
    print driver.title

finally:
    driver.quit()
